import CardContainer from "@/views/checkStatisticsPage/components/cardContainer/index.vue";
import {isArr} from "@/utils/arrayUtil";

const statisticsPageMixins = {
  components: {
    CardContainer,
  },
  props: {
    inspectInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    orgItem: {
      type: Object
    }
  },
  data() {
    return {
      orderByParam: {
        orderByColumn: null,
        isAsc: null,
        reloadTable: true,
      },
    }
  },
  watch: {
    orgItem: {
      handler(val) {
        this.refresh()
      },
      deep: true,
    }
  },
  methods: {
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      if (column.order != null && column.order !== '') {
        this.orderByParam.orderByColumn = column.prop;
        if (column.order.startsWith('asc')) {
          this.orderByParam.isAsc = 'asc'
        }
        if (column.order.startsWith('desc')) {
          this.orderByParam.isAsc = 'desc'
        }
      } else {
        this.orderByParam.orderByColumn = null
        this.orderByParam.isAsc = null;
      }
      this.getStatisticsData(this.orderByParam);
    },
    refresh() {
      this.getStatisticsData({})
    },
    getStatisticsData(param) {
      let {orgItem} = this
      let orgCode = orgItem?.value
      let orgType = orgItem?.type
      let {inspectInfo: {id: inspectId}} = this
      let queryParam = {inspectId, orgCode, orgType}
      queryParam = param != null ? {...queryParam, ...param} : queryParam;
      let owner = this
      owner.startLoading('正在加载....')
      this.queryFun(queryParam).then(response => {
          let rows = response?.rows || response?.data
          owner.statisticsData = owner.changeRowData && typeof owner.changeRowData === 'function' ? owner.changeRowData(rows) : rows;
          owner.reloadTable = false
          setTimeout(() => {
            owner.reloadTable = true
            owner.endLoading()
          }, 2)
        }
      ).catch((e) => {
        owner.endLoading()
      }).finally(() => {
      });
    }
  },
  created() {
    this.getStatisticsData({})
  }
}

export {statisticsPageMixins}
